package com.example.serviceability.slice;

import com.example.serviceability.ForegroundService;
import com.example.serviceability.ResourceTable;
import com.example.serviceability.TestService;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityConnection;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.bundle.ElementName;
import ohos.rpc.IRemoteObject;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private Button mBtnServiceStart;
    private Button mBtnServiceStop;
    private Button mBtnServiceConnect;
    private Button mBtnServiceDisconnect;
    private Button mBtnServiceForeground;
    private Button mBtnStartIndexPage;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mBtnServiceStart = (Button) findComponentById(ResourceTable.Id_btn_service_start);
        mBtnServiceStart.setClickedListener(this);
        mBtnServiceStop = (Button) findComponentById(ResourceTable.Id_btn_service_stop);
        mBtnServiceStop.setClickedListener(this);
        mBtnServiceConnect = (Button) findComponentById(ResourceTable.Id_btn_service_connect);
        mBtnServiceConnect.setClickedListener(this);
        mBtnServiceDisconnect = (Button) findComponentById(ResourceTable.Id_btn_service_disconnect);
        mBtnServiceDisconnect.setClickedListener(this);
        mBtnServiceForeground = (Button) findComponentById(ResourceTable.Id_btn_service_foreground);
        mBtnServiceForeground.setClickedListener(this);
        mBtnStartIndexPage = (Button) findComponentById(ResourceTable.Id_btn_start_index_page);
        mBtnStartIndexPage.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {

        // 【启动服务】按钮，实现启动服务功能
        if (component.getId() == ResourceTable.Id_btn_service_start) {
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("com.example.serviceability")
                    .withAbilityName("com.example.serviceability.TestService")
                    .build();
            intent.setOperation(operation);
            startAbility(intent);
        }
        // 【停止服务】按钮，实现停止服务功能
        if (component.getId() == ResourceTable.Id_btn_service_stop) {
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("com.example.serviceability")
                    .withAbilityName("com.example.serviceability.TestService")
                    .build();
            intent.setOperation(operation);
            stopAbility(intent);
        }
        // 【连接服务】按钮，实现连接服务功能
        if (component.getId() == ResourceTable.Id_btn_service_connect) {
            // TestService的Intent对象
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("com.example.serviceability")
                    .withAbilityName("com.example.serviceability.TestService")
                    .build();
            intent.setOperation(operation);
            // 连接Service
            connectAbility(intent, connection);
        }
        // 【断开连接服务】按钮，实现断开连接服务功能
        if (component.getId() == ResourceTable.Id_btn_service_disconnect) {
            // 断开连接Service
            disconnectAbility(connection);
        }
        // 【启动前台Service】按钮，实现启动前台Service功能
        if (component.getId() == ResourceTable.Id_btn_service_foreground) {
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("com.example.serviceability")
                    .withAbilityName(ForegroundService.class.getName())
                    .build();
            intent.setOperation(operation);
            startAbility(intent);
        }
        // 【打开index页面】按钮，打开index页面功能
        if (component.getId() == ResourceTable.Id_btn_start_index_page) {
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("com.example.serviceability")
                    .withAbilityName("com.example.serviceability.JSPageAbility")
                    .build();
            intent.setOperation(operation);
            startAbility(intent);
        }

    }

    // 连接远程的Service的IAbilityConnection对象
    private IAbilityConnection connection = new IAbilityConnection() {
        @Override
        public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int i) {
            // 通过远程对象操纵Service
            TestService.MyRemoteObject object = (TestService.MyRemoteObject) iRemoteObject;
            object.manipulateService();
        }

        @Override
        public void onAbilityDisconnectDone(ElementName elementName, int i) {

        }
    };
}
